//
//  HistoryTable.swift
//  BookStore
//
//  Created by JinYuXin on 2022/5/13.
//

import Foundation
class HistoryTable{
    
    static func initTable(){
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        let createSql = "CREATE TABLE IF NOT EXISTS historylist('userid'  VARCHAR(10),'listid' VARCHAR(30),'bookid' TEXT,'time' VAECHER(30),'num' VARCHAR(4),'price' VARCHAR(10));"
        if !sqlite.execNoneQuerySQL(sql: createSql){sqlite.closeDB() ; return}
        sqlite.closeDB()
    }

    static func InsertHistoryTable(historylist:HistoryList){
        let sqlite = SQLiteManager.sharedInstance
        if !sqlite.openDB(){return}
        let update = "INSERT INTO historylist(userid,listid,bookid,time,num,price) VALUES('"+historylist.userid+"','"+historylist.historyid+"','"+historylist.bookids+"','"+historylist.listtime+"','"+historylist.num+"','"+historylist.price+"');"
        if !sqlite.execNoneQuerySQL(sql: update){sqlite.closeDB() ; return}
        
        sqlite.closeDB()
    }
    
    static func GetHistoryList(){
        
        let sqlite = SQLiteManager.sharedInstance
        
        if !sqlite.openDB(){return}
        
        let queryResult = sqlite.execQuerySQL(sql: "SELECT * FROM historylist")
        
        print(queryResult!)
        
        sqlite.closeDB() ;
    }
    static func getListInfo(_ userid:String)->[[String:Any]]?
    {
        
        let sqlite = SQLiteManager.sharedInstance
        sqlite.openDB()
        let queryResult = sqlite.execQuerySQL(sql: "SELECT * FROM historylist WHERE userid='"+userid+"';")

        sqlite.closeDB();
        return queryResult
    }
    

    
}
